package com.zzyl.nursing.service.impl;

import java.util.Arrays;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zzyl.common.utils.bean.BeanUtils;
import com.zzyl.nursing.domain.NursingProjectPlan;
import com.zzyl.nursing.dto.NursingPlanDto;
import com.zzyl.nursing.mapper.NursingProjectPlanMapper;
import com.zzyl.nursing.service.INursingProjectPlanService;
import com.zzyl.nursing.vo.NursingPlanVo;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.NursingPlanMapper;
import com.zzyl.nursing.domain.NursingPlan;
import com.zzyl.nursing.service.INursingPlanService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 护理计划Service业务层处理
 *
 * @author ruoyi
 * @date 2025-02-22
 */
@Service
public class NursingPlanServiceImpl extends ServiceImpl<NursingPlanMapper, NursingPlan> implements INursingPlanService
{
    @Autowired
    private NursingPlanMapper nursingPlanMapper;
    @Autowired
    private INursingProjectPlanService nursingProjectPlanService;
    @Autowired
    private NursingProjectPlanMapper nursingProjectPlanMapper;
    /**
     * 查询护理计划
     *
     * @param id 护理计划主键
     * @return 护理计划
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public NursingPlanVo selectNursingPlanById(Integer id)
    {
        //查询护理计划基本信息-
        NursingPlan nursingPlan = getById(id);
        NursingPlanVo nursingPlanVo = new NursingPlanVo();
        BeanUtils.copyProperties(nursingPlan,nursingPlanVo);
        //查询护理计划项目关联集合
       List<NursingProjectPlan>  nursingProjectPlan = nursingProjectPlanMapper.queryById(id);
       nursingPlanVo.setProjectPlans(nursingProjectPlan);
       return nursingPlanVo;
    }

    /**
     * 查询护理计划列表
     *
     * @param nursingPlan 护理计划
     * @return 护理计划
     */
    @Override
    public List<NursingPlan> selectNursingPlanList(NursingPlan nursingPlan)
    {
        return nursingPlanMapper.selectNursingPlanList(nursingPlan);
    }

    /**
     * 新增护理计划
     *
     * @param nursingPlanDto 护理计划
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertNursingPlan(NursingPlanDto nursingPlanDto)
    {
        //新增护理计划
        NursingPlan nursingPlan = new NursingPlan();
        BeanUtils.copyProperties(nursingPlanDto,nursingPlan);
        save(nursingPlan);
        //新增护理计划和项目关联对象
        List<NursingProjectPlan> nursingProjectPlans = nursingPlanDto.getProjectPlans();
        nursingProjectPlans.forEach(t->t.setPlanId(nursingPlan.getId()));
        nursingProjectPlanService.saveBatch(nursingProjectPlans);
        return 1;
    }

    /**
     * 修改护理计划
     *
     * @param nursingPlanDto 护理计划
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateNursingPlan(NursingPlanDto nursingPlanDto)
    {
        //修改护理计划
        NursingPlan nursingPlan = new NursingPlan();
        BeanUtils.copyProperties(nursingPlanDto,nursingPlan);
        updateById(nursingPlan);
        //删除护理计划项目关联集合
        nursingProjectPlanMapper.deleteByPlanId(nursingPlan.getId());
        //新增护理计划和项目关联对象
        List<NursingProjectPlan> nursingProjectPlans = nursingPlanDto.getProjectPlans();
        nursingProjectPlans.forEach(t->t.setPlanId(nursingPlan.getId()));
        nursingProjectPlanService.saveBatch(nursingProjectPlans);

       return 1;
    }

    @Override
    public List<NursingPlan> getAllNursingPlans() {
        LambdaQueryWrapper<NursingPlan> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(NursingPlan::getStatus, 1);
        return list(queryWrapper);
    }

    /**
     * 删除护理计划信息
     *
     * @param id 护理计划主键
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteNursingPlanById(Integer id)
    {
        //删除护理计划项目关联集合
        nursingProjectPlanMapper.deleteByPlanId(id);
        //删除护理计划
        return removeById(id)?1:0;
    }

}
